const SQLiteDB = require('../config/sqlite');
const path = require('path');
const fs = require('fs');

/**
 * 初始化工作
 */
const init = () => {
    // 创建pdfZip文件夹用于存放pdf的压缩包
    mkPdfZip();
    // 创建sqlite数据库
    createDataTable();
}

/**
 * 创建数据库
 */
const createDataTable = () => {
    const db = new SQLiteDB();
    const columns = [
        { name: 'id', type: 'STRING PRIMARY KEY' },
        { name: 'course_name', type: 'STRING' },
        { name: 'class_name', type: 'STRING' },
        { name: 'progress', type: 'STRING' },
        { name: 'created_at', type: 'DATETIME DEFAULT CURRENT_TIMESTAMP' },
        { name: 'updated_at', type: 'DATETIME DEFAULT CURRENT_TIMESTAMP' }
    ];
    db.createTable('zip_progress', columns);
}

const mkPdfZip = () => {
    const localPath = path.join(__dirname, '../pdfZip');
    if(!fs.existsSync(localPath)) {
        fs.mkdirSync(localPath);
    }
}

module.exports = init;
